var util = require('../../../../utils/util.js');
var app = getApp();
var one_id;
var two_id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choose: 0,  //一级分类选择索引
    leftChoose: 0,  //二级分类选择索引
    isjicheng: false,  //成功继承弹窗
    isjichengall:false,  //继承模板弹窗
    isChooseAll: false, //全选
    isguige: [],  //规格显隐
    count: 0,  //选择数量
    isopacity:0,
    page: 1,  //当前页码
    allpage:1,
    load: "加载中...",
    stopstart: false,
    sole:0,  //检测上拉是否触底  0未触底 1触底
    isshowmore:1,  //二级更多,1展开平铺 2展开绝对定位 3收起
    loading:1
  },
  noneEnoughPeople:function(){
    return;
  },
  // 显示规格
  showspecs: function (e) {
    this.setData({
      isguige: []
    });
    var index = e.currentTarget.id;
    this.setData({
      ["isguige[" + index + "]"]: true
    })
  },
  // 隐藏规格
  hidespecs: function (e) {
    var index = e.currentTarget.id;
    this.setData({
      ["isguige[" + index + "]"]: false
    })
  },
  // 二级显示
  showmore:function(){
    var isshowmore=this.data.isshowmore;
    this.setData({
      isshowmore:isshowmore==3?2:3,
      isopacity:1,
      opac:1
    });
  },
  // 单选
  checkboxMerge: function (e) {
    var count = this.data.count;
    var newrightDatas = this.data.rightDatas;
    var index = e.currentTarget.dataset.index;
    newrightDatas[index].is_checked = !newrightDatas[index].is_checked;
    if (newrightDatas[index].is_checked == true) {
      count++;
    } else {
      count--;
    }
    if (count == newrightDatas.length) {
      this.setData({
        isChooseAll: true
      })
    } else {
      this.setData({
        isChooseAll: false
      })
    } 
    for (var i = 0; i < newrightDatas[index].specs_list.length; i++) {
      newrightDatas[index].specs_list[i].is_checked = newrightDatas[index].is_checked;
    }
    this.setData({
      rightDatas: newrightDatas,
      count: count
    })
  },
  // 全选
  chooseAll: function () {
    var count = this.data.count;
    var isc = this.data.isChooseAll;
    var newrightDatas = this.data.rightDatas;
    for (var i = 0; i < newrightDatas.length; i++) {
      newrightDatas[i].is_checked = !isc;
      for (var j = 0; j < newrightDatas[i].specs_list.length; j++) {
        newrightDatas[i].specs_list[j].is_checked = newrightDatas[i].is_checked;
      }
    }
    count = !isc == true ? newrightDatas.length : 0
    this.setData({
      rightDatas: newrightDatas,
      isChooseAll: !isc,
      count: count
    })
  },
  // 表单提交  // 继承产品底部弹出框
  formSubmit: function (e) {
    var that = this;
    var val = e.detail.value;
    var typesmove = that.data.typesmove;
    util.showBuyModal(this);
    this.setData({
      showModalStatus:true,
      ids: e.detail.value.shoptype
    })
    app.AppHttp('api/Supplier/CateList', {
      openid: app.globalData.openid,
      id: app.globalData.loginInfo.id,
      page: that.data.page,
      limit: 20,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data));
      if (res.data.two_class_list.length == 0) {
        that.setData({
          typesmove: res.data.one_class_list,
          leftTypesmove: [''],
          one_class: res.data.one_class_list[0].private_one_class,
          two_class: -1
        })
        return;
      }
      that.setData({
        typesmove: res.data.one_class_list,
        leftTypesmove: res.data.two_class_list,
        one_class: res.data.one_class_list[0].private_one_class,
        two_class: res.data.two_class_list[0].private_one_class,
        one_classname: res.data.one_class_list[0].private_class_name,
        two_classname: res.data.two_class_list[0].private_class_name
      });     
    })
  },
  // 关闭继承产品底部弹出框
  shopcancel:function(){
    util.hideBuyModal(this,"showModalStatus");
  },
  // 继承模板弹窗
  tosucceedclass:function(){
    this.setData({
      isjichengall: true
    })
  },
  // 取消关闭继承目录弹窗
  succeedcancel: function () {
    this.setData({
      isjichengall: false
    })
  },
  // 确认并关闭继承目录弹窗
  succeedconfirm: function () {
    var that = this;
    console.log(app.globalData.loginInfo.id);
    console.log(that.data.id);
    wx.showLoading({
      title:'继承模板中...'
    })
    app.AppHttp('api/Supplier/extendTemplatePublicToTemplate', {
      id: app.globalData.loginInfo.id,
      tp_id: that.data.id,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data));
      app.globalData.shop_manage= 1;
      wx.hideLoading()
      wx.showToast({
        title: res.data.msg
      })
      that.setData({
        isjichengall: false
      })
      wx.switchTab({
        url: '/pages/shangpin/shangpin'
      })
    })
  },

  // 底部弹窗选择改变
  bindChange: function (e) {
    var that = this;
    var val = e.detail.value;
    var typesmove = that.data.typesmove;
    console.log(val);
    app.AppHttp('api/Supplier/CateList', {
      openid: app.globalData.openid,
      id: app.globalData.loginInfo.id,
      page: that.data.page,
      limit: 20,
      one_id: typesmove[val[0]].private_one_class,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data))
      if (val[1] > res.data.two_class_list.length - 1) {
        val[1] = res.data.two_class_list.length - 1
      }
      if (res.data.two_class_list.length == 0) {
        that.setData({
          leftTypesmove: [''],
          one_class: res.data.one_class_list[val[0]].private_one_class,
          two_class: -1
        })
        return;
      }
      that.setData({
        leftTypesmove: res.data.two_class_list,
        one_class: res.data.one_class_list[val[0]].private_one_class,
        two_class: res.data.two_class_list[val[1]].private_one_class,
        one_classname: res.data.one_class_list[val[0]].private_class_name,
        two_classname: res.data.two_class_list[val[1]].private_class_name
      });
    })
  },
  // 继承完成产品弹窗
  tojicheng: function () {
    var that = this;
    if (that.data.two_class == -1) {
      wx.showToast({
        title: '没有二级分类'
      })
      return;
    };
    app.AppHttp('api/Supplier/extendGoodsToTemplate', {
      openid: app.globalData.openid,
      ids: that.data.ids,
      one_class: that.data.one_class,
      two_class: that.data.two_class,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data));
      app.globalData.shop_manage = 1;
      wx.showToast({
        title:res.data.msg
      })
      if (res.data.status==1){
        that.setData({
          isjicheng: true,
          count:that.data.count
        })
      }    
    })
  },
  // 返回继续操作
  jichengcancel:function(){
    this.setData({
      isjicheng: false
    })
    util.hideBuyModal(this,"showModalStatus");
  },
  // 去到目标目录
  tojichengmb: function () {
    wx.navigateBack({
      delta:2
    });
  },
  //点击切换
  mySelect: function (e) {
    this.setData({
      firstPerson: e.target.dataset.me,
      value: e.target.dataset.value,
      selectPerson: true,
      selectArea: false,
    })
  },
  // 一级分类切换
  typeClick: function (event) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    let offsetTop = event.currentTarget.offsetTop;
    this.setData({
      scrollLeft: offsetTop - this.data.height/2,
      sole:0,
      choose: event.currentTarget.dataset.id,
      leftChoose: 0,
      isshowmore:1,
      isopacity:0,
      opac:0,
      page: 1,
      isChooseAll: false,
      count: 0,
      isguige: [],
      load: "加载中..."
    });
    var that = this;
    one_id = that.data.types[that.data.choose].private_one_class;
    two_id = 0;
    that.upshoplist();
  },
  // 二级分类切换
  leftClick: function (event) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    var that = this;
    that.setData({
      sole:0,
      leftChoose: event.currentTarget.id,
      page: 1,
      isChooseAll: false,
      isshowmore:this.data.isshowmore==1?1:3,
      isopacity:event.currentTarget.dataset.opac,
      opac:event.currentTarget.dataset.opac,
      count: 0,
      isguige: [],
      load: "加载中..."
    });
    one_id = that.data.types[that.data.choose].private_one_class;
    two_id = that.data.leftTypes[that.data.leftChoose].private_one_class;
    that.upshoplist();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    one_id = 0;
    two_id = 0;
    this._observer = wx.createIntersectionObserver(this,{thresholds:[0,0.2,0.4,0.6,0.7,0.8,1]})
    this._observer
      .relativeTo('.shop_list')
      .observe('.empty_90', (res) => {
        if(this.data.isopacity==0){
          this.setData({
            isshowmore:res.intersectionRatio>0?3:1,
            opac:res.intersectionRatio
          });
        };   
    });
    this.setData({
      id: options.id
    });
    this.upshoplist();
    var height;
    const query = wx.createSelectorQuery();
      query.select('.choose-bottom').boundingClientRect();
      query.exec((res) => {
        console.log(res);
        height=res[0].top-20; 
        this.setData({
          height:height
        })
    });
  },
  upshoplist: function () {
    if(this.data.page==1){
      wx.showLoading({
        title: this.data.load,
      });
    };
    var that = this;
    app.AppHttp('api/Supplier/public_template_goods', {
      openid: app.globalData.openid,
      id:that.data.id,
      page: that.data.page,
      limit: 20,
      one_id: one_id,
      two_id: two_id,
      e_type:'client'
    }, function (res) {
      console.log(JSON.stringify(res.data))
      that.setData({
        loading:0, 
        types: res.data.one_class_list,
        leftTypes: res.data.two_class_list,
        allpage: res.data.goodList[1],
        template_count: res.data.template_count
      });
      if(that.data.page==1){
        that.setData({
          rightDatas: res.data.goodList[0]
        });
      }else{
        that.setData({
          rightDatas: that.data.rightDatas.concat(res.data.goodList[0])
        });
      }
      wx.hideLoading();
    })
  },
  touchStart: function (e) {
    let sx = e.touches[0].pageX;
    let sy = e.touches[0].pageY;
    this.data.touchS = [sx, sy];
    this.setData({
      is_start: true
    });
  },
  touchMove: function (e) {
    let sx = e.touches[0].pageX;
    let sy = e.touches[0].pageY;
    this.data.touchE = [sx, sy];
    this.setData({
      is_move: true
    });
  },
  touchEnd: function (e) {
    if (this.data.is_start == true && this.data.is_move == true) {
      let start = this.data.touchS;
      let end = this.data.touchE;
      var t_sy=end[1] - start[1]; //y轴滑动方向及距离
      if(t_sy<-150 && this.data.sole==1){
        // this.next_class();
      };   
      this.setData({
        is_start: false,
        is_move: false
      });
    }
  },
  onPageScroll: function (e) {
    var that = this;
    this.setData({
      stopstart: true
    })
    if (this.scrollEndTimer) {
      clearTimeout(this.scrollEndTimer);
      this.scrollEndTimer = null;
    }
    this.scrollEndTimer = setTimeout(function () {
      // console.log('滑动结束');
      that.setData({
        stopstart: false
      });
    }, 500);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady(){
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if(this._observer) this._observer.disconnect();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.page < that.data.allpage) {
      that.setData({
        page: that.data.page + 1,
        isChooseAll: false,
        loading:1,  //正在加载
        load: '加载更多'
      });
      that.upshoplist();
    } else {
      wx.showToast({
        title: '没有了'
      });
      this.setData({
        loading:0,
        sole:1
      }); 
    }
  },
  // 上拉加载下一个分类
  next_class:function(){
    if(parseInt(this.data.leftChoose)+1<this.data.leftTypes.length){  //最后一页二级菜单自动切换下一个
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 0
      });
      var leftChoose=parseInt(this.data.leftChoose)+1;
      this.setData({
        sole:0,
        leftChoose: leftChoose,
        page: 1,
        isChooseAll: false,
        count: 0,
        isguige: [],
        load: "加载中..."
      }); 
      one_id = this.data.types[this.data.choose].private_one_class;
      two_id = this.data.leftTypes[leftChoose].private_one_class;
      this.upshoplist();
    };
  }
})